﻿/**
* 
* @author ZhangHuihua@msn.com
* @param {Object} opts Several options
*/
(function($) {
    $.fn.extend({
        pagination: function(opts) {
            var setting = {
                first$: "li.j-first", prev$: "li.j-prev", next$: "li.j-next", last$: "li.j-last", nums$: "li.j-num>a", jumpto$: "li.jumpto",
                pageNumFrag: '<li class="#liClass#"><a href="#href#" onclick="#click#">#pageNum#</a></li>'
            };
            return this.each(function() {
                var $this = $(this);
                var pc = new Pagination(opts);
                var interval = pc.getInterval();

                var pageNumFrag = '';
                for (var i = interval.start; i < interval.end; i++) {
                    pageNumFrag += setting.pageNumFrag.replaceAll("#pageNum#", i)
                    .replaceAll("#liClass#", i == pc.getCurrentPage() ? 'selected j-num' : 'j-num')
                    .replaceAll("#href#", pc.getHref().replaceAll("#pageNum#", i))
                    .replaceAll("#click#", pc.getClick().replaceAll("#pageNum#", i));
                }
                $this.html(bee.frag["pagination"]
                    .replaceAll("#pageNumFrag#", pageNumFrag)
                    .replaceAll("#currentPage#", pc.getCurrentPage())
                    .replaceAll("#hreffirst#", pc.getHref().replaceAll("#pageNum#", 1))
                    .replaceAll("#clickfirst#", pc.getClick().replaceAll("#pageNum#", 1))
                    .replaceAll("#hrefprev#", pc.getHref().replaceAll("#pageNum#", pc.getCurrentPage() - 1))
                    .replaceAll("#clickprev#", pc.getClick().replaceAll("#pageNum#", pc.getCurrentPage() - 1))

                    .replaceAll("#hrefnext#", pc.getHref().replaceAll("#pageNum#", pc.getCurrentPage() + 1))
                    .replaceAll("#clicknext#", pc.getClick().replaceAll("#pageNum#", pc.getCurrentPage() + 1))

                    .replaceAll("#hreflast#", pc.getHref().replaceAll("#pageNum#", pc.numPages()))
                    .replaceAll("#clicklast#", pc.getClick().replaceAll("#pageNum#", pc.numPages()))

                    .replaceAll("#clickjump#", pc.getClick().replaceAll("#pageNum#", "0, this"))
                    ).find("li").hoverClass();

                var $first = $this.find(setting.first$);
                var $prev = $this.find(setting.prev$);
                var $next = $this.find(setting.next$);
                var $last = $this.find(setting.last$);

                if (pc.hasPrev()) {
                    $first.add($prev).find(">span").hide();

                } else {
                    $first.add($prev).addClass("disabled").find(">a").hide();
                }

                if (pc.hasNext()) {
                    $next.add($last).find(">span").hide();

                } else {
                    $next.add($last).addClass("disabled").find(">a").hide();
                }

                $this.find(setting.jumpto$).each(function() {
                    var $this = $(this);
                    var $inputBox = $this.find(":text");
                    var $button = $this.find(":button");

                    $inputBox.keyup(function(event) {
                        if (event.keyCode == bee.keyCode.ENTER) $button.click();
                    });
                });
            });
        },

        orderBy: function(options) {
            var op = $.extend({ pageId: "", asc: "asc", desc: "desc" }, options);
            return this.each(function() {
                var $this = $(this).css({ cursor: "pointer" }).click(function() {
                    var orderField = $this.attr("orderField");
                    var orderDirection = $this.hasClass(op.asc) ? op.desc : op.asc;

                    autoList(op.pageId, orderField, orderDirection);
                });

            });
        }
    });

    var Pagination = function(opts) {
        this.opts = $.extend({
            targetType: "navTab", // navTab, dialog
            totalCount: 0,
            numPerPage: 10,
            pageNumShown: 10,
            currentPage: 1,
            href: "javascript:;",
            click: "javascript:;",
            callback: function() { return false; }
        }, opts);
    }

    $.extend(Pagination.prototype, {
        targetType: function() { return this.opts.targetType },
        numPages: function() {
            return Math.ceil(this.opts.totalCount / this.opts.numPerPage);
        },
        getInterval: function() {
            var ne_half = Math.ceil(this.opts.pageNumShown / 2);
            var np = this.numPages();
            var upper_limit = np - this.opts.pageNumShown;
            var start = this.getCurrentPage() > ne_half ? Math.max(Math.min(this.getCurrentPage() - ne_half, upper_limit), 0) : 0;
            var end = this.getCurrentPage() > ne_half ? Math.min(this.getCurrentPage() + ne_half, np) : Math.min(this.opts.pageNumShown, np);
            return { start: start + 1, end: end + 1 };
        },
        getCurrentPage: function() {
            var currentPage = parseInt(this.opts.currentPage);
            if (isNaN(currentPage)) return 1;
            return currentPage;
        },
        hasPrev: function() {
            return this.getCurrentPage() > 1;
        },
        hasNext: function() {
            return this.getCurrentPage() < this.numPages();
        },
        getHref: function() {
            return this.opts.href;
        },
        getClick: function() {
            return this.opts.click;
        }
    });
})(jQuery);
